package com.joint.web.action.app;

import com.fz.us.base.bean.Result;
import com.fz.us.base.util.DataUtil;
import com.fz.us.base.util.LogUtil;
import com.joint.core.entity.ProInfo;
import com.joint.core.entity.ProLeave;
import com.joint.core.service.ProInfoService;
import com.joint.core.service.ProLeaveService;
import com.joint.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 后台Action类 - 项目请假管理
 */
@ParentPackage("app")
public class ProleaveAction extends BaseAction implements ModelDriven<ProLeave> {

    private static final long serialVersionUID = -4787241461181578464L;

    private ProLeave proleave;
	private List<ProLeave> proLeaveList;

    private ProInfo proInfo;
    private String proinfoId;
    private String strLeaveDate;
    private String strReason;

    @Resource
    private ProInfoService proInfoService;

    @Resource
    private ProLeaveService proLeaveService;

	/**
	 * 请假信息详情
     *
	 * @return String
	 * @throws Exception
	 */
	public String getDetail() throws Exception {

        if (StringUtils.isNotEmpty(keyId)) {
            proleave = proLeaveService.get(keyId);
            if (proleave != null) {
                Map<String, Object> data = proLeaveService.getDetailMap(proleave);
                return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
            }
        }
        return ajaxHtmlAppResult(0, "无法找到该文档，可能已被删除！", null);
	}

    /**
     * 初始化请假详情
     *
     * @return String
     * @throws Exception
     */
    public String initDetail() throws Exception {
        users = getRequestUsers();
        company = users.getCompany();
        if(company == null)
            return ajaxHtmlAppResult(0, "未找到用户所属企业！",null);

        if (StringUtils.isNotEmpty(proinfoId)) {

            proInfo = proInfoService.get(proinfoId);
            if(proInfo == null)
                return ajaxHtmlAppResult(0, "未找到所属项目！",null);

            //如果不是该项目的总监或项目组成员，则不允许新建
//            int flg = 0;
//            if (users.equals(proInfo.getChief())){
//                flg = 1;
//            }else {
//                Set<Users> usersSet = proInfo.getGroup();
//                if (usersSet!=null && usersSet.size()>0) {
//                    if (usersSet.contains(users)) {
//                        flg = 1;
//                    }
//                }
//            }
//            LogUtil.info("flg:" + flg);
//            if(flg==0){
//                return ajaxHtmlAppResult(0, "您不是该项目的总监或项目组成员，不允许新建！",null);
//            }

            proleave = new ProLeave();
            proleave.setProInfo(proInfo);

            Map<String, Object> data = proLeaveService.getDetailMap(proleave);
            LogUtil.info("data:" + data);
            return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
        }

        return ajaxHtmlAppResult(0, "初始化信息失败！", null);
    }

    /**
     * 请假信息创建
     *
     * @return String
     * @throws Exception
     */
    public String create() throws Exception {

        users = getRequestUsers();
        company = users.getCompany();
        if(company == null)
            return ajaxHtmlAppResult(0, "未找到用户所属企业！",null);

        if(StringUtils.isNotEmpty(proinfoId)){
            proInfo = proInfoService.get(proinfoId);
        }
        if(proInfo == null){
            return ajaxHtmlAppResult(0, "未找到所属项目！",null);
        }
        if(StringUtils.isEmpty(strReason)){
            return ajaxHtmlAppResult(0, "请假事由不能为空！",null);
        }

        if(StringUtils.isEmpty(strLeaveDate)){
            return ajaxHtmlAppResult(0, "请假日期不能为空！",null);
        }

        proleave.setProInfo(proInfo);
        proleave.setName(proInfo.getName());
        proleave.setLeaveDate(DataUtil.StringToDate(strLeaveDate + " 12:00", "yyyy-MM-dd HH:mm"));
        proleave.setReason(strReason);
        proleave.setCreater(users);
        proleave.setCompany(company);
        proLeaveService.updateAndEnable(proleave);

        LogUtil.info("{Proleave}"+strReason);
        Result result = new Result(1, proleave.getId());
        return ajaxHtmlAppResult(result);
    }

	@Override
	public ProLeave getModel() {
		if(proleave == null){
            proleave = new ProLeave();
		}
		return proleave;
	}

    public String getProinfoId() {
        return proinfoId;
    }

    public void setProinfoId(String proinfoId) {
        this.proinfoId = proinfoId;
    }

    public String getStrLeaveDate() {
        return strLeaveDate;
    }

    public void setStrLeaveDate(String strLeaveDate) {
        this.strLeaveDate = strLeaveDate;
    }

    public String getStrReason() {
        return strReason;
    }

    public void setStrReason(String strReason) {
        this.strReason = strReason;
    }
}
